package com.cyj.dream.core.util;

import com.cyj.dream.core.constant.enums.ResultCode;
import com.cyj.dream.core.constant.security.Constants;
import io.swagger.annotations.ApiModelProperty;
import lombok.*;
import lombok.experimental.Accessors;

import java.io.Serializable;

/**
 * 响应信息主体
 *
 * @param <T>
 * @author sos
 */
@ToString
@NoArgsConstructor
@AllArgsConstructor
@Accessors(chain = true)
public class R<T> implements Serializable {

	private static final long serialVersionUID = 1L;

	@Getter
	@Setter
	@ApiModelProperty(value = "返回状态值")
	private int code;

	@Getter
	@Setter
	private String msg;

	@Getter
	@Setter
	private Boolean success;

	@Getter
	@Setter
	private T data;

	public static <T> R<T> ok() {
		return restResult(null, Constants.SUCCESS, null,true);
	}

	public static <T> R<T> ok(T data) {
		return restResult(data, Constants.SUCCESS, null,true);
	}

	public static <T> R<T> ok(T data, String msg) {
		return restResult(data, Constants.SUCCESS, msg,true);
	}

	public static <T> R<T> failed() {
		return restResult(null, Constants.FAIL, null,false);
	}

	public static <T> R<T> failed(String msg) {
		return restResult(null, Constants.FAIL, msg,false);
	}

	public static <T> R<T> failed(T data) {
		return restResult(data, Constants.FAIL, null,false);
	}

	/**
	 * 对异常提示语进行封装
	 */
	public static <T> R<T>  failed(ResultCode resultCode, String message) {
		R fail = R.fail(resultCode);
		fail.setMsg(message);
		return fail;
	}

	/**
	 * 对异常枚举进行封装
	 */
	public static <T> R<T> fail(ResultCode resultCode) {
		return restResult(null, resultCode.code(), resultCode.message(),false);
	}

	public static <T> R<T> failed(int code, String msg) {
		return restResult(null, code, msg,false);
	}
	private static <T> R<T> restResult(T data, int code, String msg,Boolean success) {
		R<T> apiResult = new R<>();
		apiResult.setCode(code);
		apiResult.setData(data);
		apiResult.setMsg(msg);
		apiResult.setSuccess(success);
		return apiResult;
	}



}
